package com.zb.jnlxc.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.ZLHW.base.Form.Page;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.zb.jnlxc.form.MiniPageRsp;
import com.zb.jnlxc.model.Admin;
import com.zb.jnlxc.model.PaichanOrderDetail;
import com.zb.jnlxc.service.PaichanRecordService;
import com.zb.jnlxc.service.ProduceFlowService;

/**
 * Created by zhengbo.zb on 2014/9/17.
 */
@Controller
@RequestMapping("/ProduceFlowAction")
@SessionAttributes(value = { "user" })
public class ProduceFlowAction {
    Logger                       logger = LoggerFactory.getLogger(ProduceFlowAction.class);
    @Resource
    private ProduceFlowService   produceFlowService;
    @Resource
    private PaichanRecordService paichanRecordService;

    /**
     * 获取生产任务
     * @return
     */
    @ResponseBody
    @RequestMapping("/loadLiaoKuang")
    public MiniPageRsp loadLiaoKuang(Page page, String code, String currentStep,
                                     @ModelAttribute("user") Admin admin) {
        produceFlowService.loadLiaoKuang(page, code, currentStep);
        return new MiniPageRsp(page.getResultData(), page.getTotalClum());
    }

    @ResponseBody
    @RequestMapping("/getDetailByLiaoKuang")
    public MiniPageRsp getDetailByLiaoKuang(Integer liaoKuangId) {
        return new MiniPageRsp(produceFlowService.getDetailByLiaoKuang(liaoKuangId));
    }

    @ResponseBody
    @RequestMapping("/getShiXiaoByLiaoKuang")
    public MiniPageRsp getShiXiaoByLiaoKuang(Integer liaoKuangId) {
        return new MiniPageRsp(produceFlowService.getShiXiaoByLiaoKuang(liaoKuangId));
    }

    /**
     * 获取机台生产情况（相同图纸未需要使用（未生产）的情况）
     */
    @ResponseBody
    @RequestMapping("/getProductSituation")
    public MiniPageRsp getProductSituation(Page page, Integer mouldId) {
        produceFlowService.getProductSituation(page, mouldId);
        return new MiniPageRsp(page.getResultData(), page.getTotalClum());
    }

    /**
     * 获取料框理论重量
     * @param liaoKuangId
     * @return
     */
    @ResponseBody
    @RequestMapping("/getLiaoKuangWeight")
    public Integer getLiaoKuangWeight(Integer liaoKuangId) {
        return produceFlowService.getLiaoKuangWeight(liaoKuangId);
    }

    /**
     * 获取料框理论重量
     * @param yangHuaId
     * @return
     */
    @ResponseBody
    @RequestMapping("/getYangHuaWeight")
    public float getYangHuaWeight(Integer yangHuaId) {
        return produceFlowService.getYangHuaWeight(yangHuaId);
    }

    /**
     * 料框挤压过磅
     */
    @ResponseBody
    @RequestMapping("/liaoKuangWeight")
    public void liaoKuangWeight(Integer liaoKuangId, String formData,
                                @ModelAttribute("user") Admin admin) {
        Map<String, String> form = JSONObject.parseObject(formData,
            new TypeReference<Map<String, String>>() {
            }.getType());
        produceFlowService.liaoKuangWeight(liaoKuangId, form, admin);
    }

    /**
     * 获取料框中待处理的工序
     * @param liaoKuangId
     * @return
     */
    @ResponseBody
    @RequestMapping("/getByLiaoKuang")
    public MiniPageRsp getByLiaoKuang(Integer liaoKuangId) {
        return new MiniPageRsp(produceFlowService.getByLiaoKuang(liaoKuangId));
    }

    @ResponseBody
    @RequestMapping("/getSizeList")
    public List<PaichanOrderDetail> getSizeList(Integer paiChanRecordId) {
        return paichanRecordService.getSizeList(paiChanRecordId);
    }

    @ResponseBody
    @RequestMapping("/shixiao")
    public void shixiao(Integer lastProcessId, String formData, @ModelAttribute("user") Admin admin) {
        Map<String, String> form = JSONObject.parseObject(formData,
            new TypeReference<Map<String, String>>() {
            });
        produceFlowService.shixiao(lastProcessId, form, admin);
    }

    @ResponseBody
    @RequestMapping("/yanghua")
    public void yanghua(Integer lastProcessId, String formData, @ModelAttribute("user") Admin admin) {
        Map<String, String> form = JSONObject.parseObject(formData,
            new TypeReference<Map<String, String>>() {
            });
        produceFlowService.yanghua(lastProcessId, form, admin);
    }

    @ResponseBody
    @RequestMapping("/pack")
    public void pack(Integer yangHuaId, String formData, @ModelAttribute("user") Admin admin) {
        Map<String, String> form = JSONObject.parseObject(formData,
            new TypeReference<Map<String, String>>() {
            });
        produceFlowService.pack(yangHuaId, form, admin);
    }

    @ResponseBody
    @RequestMapping("/storageFinish")
    public void storageFinish(Integer liaoKuangId, String formData,
                              @ModelAttribute("user") Admin admin) {
        Map<String, String> form = JSONObject.parseObject(formData,
            new TypeReference<Map<String, String>>() {
            });
        produceFlowService.storageFinish(liaoKuangId, form, admin);
    }

    @ResponseBody
    @RequestMapping("/shixiaoFinish")
    public void shixiaoFinish(Integer liaoKuangId, @ModelAttribute("user") Admin admin) {
        produceFlowService.shixiaoFinish(liaoKuangId, admin);
    }
}
